# .NET 项目发布脚本
# 用于生成 publish 发布文件

param(
    [string]$Configuration = "Release",
    [string]$Framework = "net6.0",
    [string]$OutputPath = ".\publish"
)

Write-Host "开始发布 .NET 项目..." -ForegroundColor Green

# 检查项目文件是否存在
$projectFile = "Chat.csproj"
if (-not (Test-Path $projectFile)) {
    Write-Host "错误: 找不到项目文件 $projectFile" -ForegroundColor Red
    exit 1
}

# 清理之前的发布文件
if (Test-Path $OutputPath) {
    Write-Host "清理之前的发布文件..." -ForegroundColor Yellow
    Remove-Item $OutputPath -Recurse -Force
}

# 执行 dotnet publish 命令
Write-Host "正在发布项目 (配置: $Configuration, 框架: $Framework)..." -ForegroundColor Cyan

try {
    dotnet publish $projectFile `
        --configuration $Configuration `
        --framework $Framework `
        --output $OutputPath `
        --self-contained false `
        --verbosity minimal

    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ 项目发布成功!" -ForegroundColor Green
        Write-Host "发布文件位置: $OutputPath" -ForegroundColor Green
        
        # 显示发布文件列表
        Write-Host "`n📁 发布文件列表:" -ForegroundColor Cyan
        Get-ChildItem $OutputPath | Format-Table Name, Length, LastWriteTime -AutoSize
    } else {
        Write-Host "❌ 项目发布失败!" -ForegroundColor Red
        exit 1
    }
} catch {
    Write-Host "❌ 发布过程中出现错误: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

Write-Host "`n🎉 发布完成!" -ForegroundColor Green